"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 备设备pg_log目录权限修改重启数据库
Create At   : 2020/07/23
Owner       : @li-xin12345
Description :
    1. 删除pg_log权限
    2. 重启数据库
    3. 恢复pg_log权限
    4.重启数据库
Expect      :
    1. 删除pg_log权限成功
    2. 重启数据库失败
    3. 无
    4.数据库可以正常启动
History     :
    modified: 2020/12/10  modified by @wan005,生成corefile不作为检查点
    modified: 2021/01/15  modified by @wan005,更改备节点pg_log权限后重启成功，
    期望失败，非问题，修改用例此处不做判断，仅恢复后数据库正常即可
    Modified by @li-xin12345 at 2023/2/20: 权限恢复后增加查看数据库状态及循环重启操作
"""

import os
import sys
import unittest
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    logger.info('------------Opengauss_Reliability_Dbsys_Case188.py start------------')
    primaryNode = Node(node='PrimaryDbUser')
    primaryRoot = Node(node='PrimaryRoot')
    standbyRoot = Node(node='Standby1Root')
    standbyNode = Node(node='Standby1DbUser')
    Constant = Constant()
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def test_Index_file_damaged(self):
        logger.info('----------------------------更改pg_log文件权限-----------------------------')
        excute_cmd = f'chmod -R 000 {macro.PG_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)

        logger.info('-------------------重启数据库-------------------------------')
        time.sleep(5)
        logger.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        result = self.sh_primy.stopDbCluster()
        logger.info(result)
        result = self.sh_primy.startDbCluster()
        logger.info(result)

        # 恢复pg_log文件权限
        logger.info('----------------------------恢复pg_log文件权限-----------------------------')
        excute_cmd = f'chmod -R 700 {macro.PG_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)

        logger.info('-------------------重启数据库-------------------------------')
        time.sleep(60)
        logger.info('查询数据库状态')
        for i in range(5):
            status = self.sh_primy.getDbClusterStatus("status")
            logger.info(status)
            if status == True:
                break
            stop_res = self.sh_primy.stopDbCluster()
            logger.info(stop_res)
            start_res = self.sh_primy.startDbCluster()
            logger.info(start_res)
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        self.assertTrue(status)

    def tearDown(self):
        logger.info('----------------------------teardown:恢复pg_log文件权限-----------------------------')
        excute_cmd = f'chmod -R 700 {macro.PG_LOG_PATH};'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)

        logger.info('-------------------重启数据库-------------------------------')
        logger.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        logger.info(status)
        result = self.sh_primy.stopDbCluster()
        logger.info(result)
        result = self.sh_primy.startDbCluster()
        logger.info(result)
        logger.info('----------------------------Reliability_Dbsys_Case188执行完成-----------------------------')
